package com.platform.system.controller.rolegroup;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.platform.common.annotations.ResponseResult;
import com.platform.common.annotations.RoleOperation;
import com.platform.common.bean.Result;
import com.platform.common.controller.BaseController;
import com.platform.system.entity.SysRoleGroup;
import com.platform.system.service.SysRoleGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * describe:角色组Api接口
 *
 * @author rainly
 * @date 2019/12/08
 * @time 7:08 下午
 */
@Api(value = "角色组controller", tags = {"角色组操作接口"})
@RestController
@ResponseResult
@RoleOperation(codes = "system:roleGroup:query,system:roleGroup:delete")
@RequestMapping(value = "/system/roleGroup")
public class RoleGroupApiController extends BaseController<SysRoleGroup> {

    @Resource
    private SysRoleGroupService sysRoleGroupService;

    /**
     * 角色组列表
     *
     * @param pageNum  当前分页
     * @param pageSize 当前分页数量
     * @return 列表
     */
    @ApiOperation(value = "角色组列表", tags = {"角色组列表"}, notes = "必须传入page和limit", httpMethod = "GET" )
    @GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object getRoleGroupList(@ApiParam(name = "page", value = "当前分页", required = true) Integer pageNum,
                                   @ApiParam(name = "limit", value = "当前分页数量", required = true) Integer pageSize) {
        if (null == pageNum || pageNum == 0) {
            pageNum = 1;
        }
        if (null == pageSize || pageSize == 0) {
            pageSize = this.pageSize;
        }
        QueryWrapper<SysRoleGroup> queryWrapper = new QueryWrapper<>();
        String roleGroupName = this.getRequest().getParameter("roleGroupName");
        String parentId = this.getRequest().getParameter("parentId");
        if (StringUtils.isNotBlank(roleGroupName)) {
            queryWrapper.like("roleGroupName", roleGroupName);
        }
        if (StringUtils.isNotBlank(parentId)) {
            queryWrapper.eq("parentId", parentId);
        }else {
            queryWrapper.eq("parentId", "top");
        }
        return PageHelper.startPage(pageNum, pageSize, true).doSelectPage(() -> sysRoleGroupService.list(queryWrapper)).toPageInfo();
    }

    /**
     * 新增角色组
     *
     * @param sysRoleGroup 新增角色组
     * @return 保存结果
     */
    @ApiOperation(value = "新增角色组", notes = "新增角色组", httpMethod = "POST")
    @PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object postRoleGroup(@ApiParam(name = "角色组对象", value = "传入json格式", required = true) @ModelAttribute @Valid SysRoleGroup sysRoleGroup, BindingResult bingdingResult) {
        if (bingdingResult.hasErrors()) {
            List<String> errorMessageList = bingdingResult.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
            return Result.failure(errorMessageList);
        } else {
            SysRoleGroup parentRoleGroup = sysRoleGroupService.getById(sysRoleGroup.getParentId());
            if (null != parentRoleGroup){
                sysRoleGroup.setParentIds(parentRoleGroup.getParentIds()+","+parentRoleGroup.getParentId());
            }
            return sysRoleGroupService.save(sysRoleGroup);
        }
    }

    /**
     * 修改角色组
     *
     * @param sysRoleGroup 角色组对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改角色", notes = "修改角色", httpMethod = "PUT")
    @PutMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object putRoleGroupById(@ApiParam(name = "角色对象", value = "传入json格式", required = true) @ModelAttribute @Valid SysRoleGroup sysRoleGroup, BindingResult bingdingResult) {
        if (bingdingResult.hasErrors()) {
            List<String> errorMessageList = bingdingResult.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.toList());
            return Result.failure(errorMessageList);
        } else {
            return sysRoleGroupService.updateById(sysRoleGroup);
        }
    }

    /**
     * 修改角色组属性
     *
     * @param sysRoleGroup 角色组对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改属性", notes = "修改属性", httpMethod = "PATCH")
    @PatchMapping(value = "/")
    public Object patchRoleGroupByid(@ApiParam(name = "param", value = "传入json格式的角色属性", required = true) @RequestBody SysRoleGroup sysRoleGroup) {
        if (StringUtils.isNotBlank(sysRoleGroup.getRoleGroupId())) {
            return sysRoleGroupService.updateById(sysRoleGroup);
        } else {
            return "id不能为空";
        }
    }

    /**
     * 根据id查询
     *
     * @param roleGroupId 角色组id
     * @return 角色组对象
     */
    @ApiOperation(value = "根据id查询角色组", notes = "根据id查询角色组", httpMethod = "GET")
    @RoleOperation(codes = {"system:roleGroup:query","system:roleGroup:edit"})
    @GetMapping(value = "/{roleGroupId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object getRoleGroupById(@PathVariable String roleGroupId) {
        return Optional.ofNullable(sysRoleGroupService.getById(roleGroupId));
    }

    /**
     * 根据id删除
     *
     * @param roleGroupId 角色组id
     * @return 是否删除成功
     */
    @ApiOperation(value = "根据id删除角色组", notes = "根据id删除角色组", httpMethod = "DELETE")
    @DeleteMapping(value = "/{roleGroupId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object delRoleGroupById(@PathVariable String roleGroupId) {
        return sysRoleGroupService.removeById(roleGroupId);
    }

    /**
     * 批量删除
     *
     * @param ids list<Stirng> ids list数组
     * @return 是否删除成功
     */
    @DeleteMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object delRoleGroupByIds(@RequestBody List<String> ids) {
        return sysRoleGroupService.removeByIds(ids);
    }
}
